package cn.semcorp.zhuhaizj.services.impl;

import cn.semcorp.zhuhaizj.exception.ServiceException;
import cn.semcorp.zhuhaizj.mapper.UserMapper;
import cn.semcorp.zhuhaizj.pojo.entity.UserEntity;
import cn.semcorp.zhuhaizj.pojo.vo.LoginUserVO;
import cn.semcorp.zhuhaizj.restful.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserServiceImpl {

    @Autowired
    private UserMapper userMapper;


    public LoginUserVO login(UserEntity userEntity){
        LoginUserVO loginUserVO = userMapper.selectUser(userEntity.getUsername());
        // 判断是否已经有人登录
        if (loginUserVO.getState().equals("1")){
            loginUserVO.setDes(2);
            return loginUserVO;
        }

        // 判断用户名和密码
        if (loginUserVO.getState().equals("0")){
            if (loginUserVO.getPassword().equals(userEntity.getPassword())){
                userMapper.updateLoginStateNow(userEntity.getUsername());
                // 1. 修改登录的时间
                //userMapper.updateLoginModified(loginUserVO.getId());
                loginUserVO.setDes(1);
                return loginUserVO;
            }
        }

        loginUserVO.setUsername("");
        loginUserVO.setPassword("");
        loginUserVO.setSrc("");
        loginUserVO.setDes(0);
        return loginUserVO;
    }

    public void uploadAvatar(String username, String base64) {
        userMapper.uploadAvatar(username,base64);
    }

    /**
     * 检查是否登录
     */
    public String selectLoginState() {
        return userMapper.selectLoginState("admin");
    }

    public int loginOut(String name) {
        return userMapper.loginOut(name);
    }
}
